<?php

// +----------------------------------------------------------------------
// |ZBPHP[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2019 http://www.benbenwangluo.com
// +----------------------------------------------------------------------
// | Author 似水星辰 [ 2630481389@qq.com ]
// +----------------------------------------------------------------------
// | 中犇软件 技术六部 出品
// +----------------------------------------------------------------------
namespace app\admin\admin;

use app\admin\admin\Base;

/**
 * AJAX插件用
 * @author 晓风<215628355@qq.com>
 * @date 2021-7-7 17:29:51
 */
class Ajax extends Base {

    /**
     * 
     * @author 晓风<215628355@qq.com>
     * @return string
     * 
     */
    public function searchUser()
    {
        // 查询
        $key = input('key/s', '');
        if ($key == '') {
            return '';
        }
        $page = input('page/d', 1);
        // 数据列表
        $data_list = \app\user\model\User::where('id', 'like', '%' . $key . '%')
            ->whereOr('user_name', 'like', '%' . $key . '%')
            ->whereOr('user_nickname', 'like', '%' . $key . '%')
            ->whereOr('mobile', 'like', '%' . $key . '%')
            ->field('id,user_name,user_nickname,mobile')
            ->paginate(20);
        $results = [];
        foreach ($data_list as $v) {
            $str = $v['id'] . '/' . $v['user_name'] . '/' . $v['user_nickname'] . '/' . $v['mobile'];
            $results[] = ['id' => $v['id'], 'text' => $str];
        }
        $data['results'] = $results;
        $data['pagination'] = $data_list->currentPage() == $data_list->lastPage() ? ['more' => true] : ['more' => false];
        return json($data, JSON_UNESCAPED_UNICODE);
    }
    
     /**
     * 手机区号组件
     * @author 晓风<215628355@qq.com>
     * @return string
     * 
     */
    public function get_mobile_prefix()
    {
        $lang = \think\facade\Lang::range();
        $field = $lang == "en-us" ? 'country_en' : 'country';
        // 查询
        $key = input('key/s', '');   
        $scene = input('scene/d', 0);   
        $map = [];
        if ($key ) {
            $map[] = ['prefix', 'like', '%' . $key . '%'];
            $map[] = [$field, 'like', '%' . $key . '%'];
        } 
      
        // 数据列表en-
        $data_list = \think\Db::name('phone_prefix')
            ->where($map)
            ->field('prefix,'.$field)
            ->order("letter asc")
            ->select();
        $results = [];
        foreach ($data_list as $v) {
          
            $str = '+' . $v['prefix'] . ' ' . $v[$field] ;
            $results[] = ['id' => $v['prefix'], 'text' => $str];
        }
        $data['results'] = $results;
        $data['pagination'] =  ['more' => false];
        return json($data, JSON_UNESCAPED_UNICODE);
    }

}
